﻿class com.criticalmass.common.drawing.shapes.Rectangle {
	
	var lineTo:Function;
	var moveTo:Function;
	var curveTo:Function;
	var beginFill:Function;
	var endFill:Function;
	var clear:Function;
	var lineStyle:Function;
	var beginGradientFill:Function;
/**
* Draws a rectangle using the Drawing API. This class must be attached to a movieclip in the library and exported for Actionscript
* You can draw unfilled or filled rectatngles by changing which parameters are passed
* @param x x position of  fill
* @param y y position of  fill
* @param w width of  fill
* @param h  height of  fill
* @param r corner radius of fill :: number or object {br:#,bl:#,tl:#,tr:#}
* @param l lineStyle :: object
* @param c hex color of fill :: number or array [0x######,0x######]
* @param alpha alpha value of fill :: number or array [0x######,0x######]
* @param gradient type of gradient "linear" or "radial"
* @param ratios (optional :: default  [0,255]) - specifies the distribution of colors :: array [#,#];
* @param rot rotation of fill :: number or matrix object  {matrixType:"box",x:#,y:#,w:#,h:#,r:(#*(Math.PI/180))}
*/
	function drawRectangle(x,y,w,h,r,l,c,alpha,gradient,ratios, rot):Void {
			if (typeof r == "object") {
				var rbr = r.br //bottom right corner
				var rbl = r.bl //bottom left corner
				var rtl = r.tl //top left corner
				var rtr = r.tr //top right corner
			}
			else
			{
				var rbr =  rbl = rtl = rtr = r;
			}
			if(l != null) {
				this.lineStyle(l.thickness, l.col, l.alpha)
			}
			// if color is an object then allow for complex fills
			if(typeof c == "object")
			{
				if (typeof alpha != "object")
					var alphas = [alpha,alpha];
				else
					var alphas = alpha;

				if (ratios == undefined || ratios == null)
					var ratios = [ 0, 0xff ];

				var sh = h *.7
				if (typeof rot != "object")
					var matrix = {matrixType:"box", x:-sh, y:sh, w:w*2, h:h*4, r:rot * 0.0174532925199433 }
				else
					var matrix = rot;

				if (gradient == "radial")
					this.beginGradientFill( "radial", c, alphas, ratios, matrix );
				else
					this.beginGradientFill( "linear", c, alphas, ratios, matrix );

			}
			else if (c != undefined)
			{
				this.beginFill (c, alpha);
			}

			// Math.sin and Math,tan values for optimal performance.
			// Math.rad = Math.PI/180 = 0.0174532925199433
			// r*Math.sin(45*Math.rad) =  (r*0.707106781186547);
			// r*Math.tan(22.5*Math.rad) = (r*0.414213562373095);

			//bottom right corner
			r = rbr;
			var a = r - (r*0.707106781186547); //radius - anchor pt;
			var s = r - (r*0.414213562373095); //radius - control pt;
			this.moveTo ( x+w,y+h-r);
			this.lineTo ( x+w,y+h-r );
			this.curveTo( x+w,y+h-s,x+w-a,y+h-a);
			this.curveTo( x+w-s,y+h,x+w-r,y+h);

			//bottom left corner
			r = rbl;
			var a = r - (r*0.707106781186547);
			var s = r - (r*0.414213562373095);
			this.lineTo ( x+r,y+h );
			this.curveTo( x+s,y+h,x+a,y+h-a);
			this.curveTo( x,y+h-s,x,y+h-r);

			//top left corner
			r = rtl;
			var a = r - (r*0.707106781186547);
			var s = r - (r*0.414213562373095);
			this.lineTo ( x,y+r );
			this.curveTo( x,y+s,x+a,y+a);
			this.curveTo( x+s,y,x+r,y);

			//top right
			r = rtr;
			var a = r - (r*0.707106781186547);
			var s = r - (r*0.414213562373095);
			this.lineTo ( x+w-r,y );
			this.curveTo( x+w-s,y,x+w-a,y+a);
			this.curveTo( x+w,y+s,x+w,y+r);
			this.lineTo ( x+w,y+h-rbr );

			if (c != undefined) {
				this.endFill();
			}
	}
/**
* The classConstructed method ensures that the MovieClip Class is extended with this new method
*/
	static function classConstruct(Void):Boolean {
		if (!Rectangle.classConstructed) {
			MovieClip.prototype.drawRectangle = Rectangle.prototype.drawRectangle;
			return true;
		}
	}
	static var classConstructed:Boolean = classConstruct();
}